
//
// 110113 - AH - Created.
// 110114 - AH - getCluster().
// 110115 - AH - (Bien) Now sets boolean variable "key" on matched nodes.
//

package org.soekrat.engine.retrieval.network;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.soekrat.engine.network.Cluster;
import org.soekrat.engine.network.Network;
import org.soekrat.engine.network.NetworkNode;
import org.soekrat.engine.visitor.Input;

/**
 * <p>
 *   Information on how a keyword matched in the network.
 * </p>
 * @author Laetitia Le Chatton (LLC) 
 * @author Arne Halvorsen (AH) 
 */
final class NetworkScanDetail 
{
  private final Cluster m_cluster;
  
  private final List<NetworkNode> m_matched=new ArrayList<NetworkNode>();
  
  private final int m_linkage;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param cluster {@link Cluster} to find matches in.
   * @param tokens  Tokens extracted from user input.
   */
  NetworkScanDetail(Cluster cluster, Input tokens)
  {
    if (cluster==null)
    {
      throw new NullPointerException("cluster");  
    }
    if (tokens==null)
    {
      throw new NullPointerException("tokens");
    }
    
    m_cluster=cluster;
   
    for (String token : tokens) 
    {
      NetworkNode node=cluster.getNode(token);
      if (node!=null)
      {
        m_matched.add(node);
        node.setAttribute("key", true);
      }
    }
    
    m_linkage=setMaximumLinkageAttribute();
  }
  
  private int setMaximumLinkageAttribute()
  {
    int max=0;
    for (NetworkNode curr : m_matched)
    {
      int l=curr.getLinkage();
      if (l>max) max=l;
    }
    
    m_cluster.setAttribute("linkage", max);
    return max;
  }
  
  /**
   * <p>
   *   Gets the
   *   {@link Cluster} {@code this} contains match info for.
   * </p>
   * @return {@code Cluster}.
   */
  Cluster getCluster(){ return m_cluster; }
  
  /**
   * <p>
   *   Gets the number of tokens matched.
   * </p>
   * @return Count.
   */
  int getNumberMatches(){ return m_matched.size(); }
  
  /**
   * <p>
   *   Gets token been matched in cluster this is a detail for.
   * </p>
   * @param tokens Where tokens are added, old content is not removed by this
   *               method so can be used to accumulate all unique tokens 
   *               matched in
   *               {@link Network}.
   */
  void getTokens(Set<String> tokens)
  {
    if (tokens==null)
    {
      throw new NullPointerException("tokens");
    }
    
    for (NetworkNode curr : m_matched)
    {
      tokens.add(curr.getName());  
    }
  }
  
  /**
   * <p>
   *   Gets maximum linkage in cluster.
   * </p>
   * @return Maximum linkage.
   */
  int getMaximumLinkage(){ return m_linkage; }
  
}
